<?php

namespace App\Admin\Exporter;

use App\Models\Family;
use App\Models\FamilyEdu;
use App\Models\FamilyType;
use App\Models\Hospital;
use App\Models\HospitalTeam;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class GuideExporter implements WithHeadings, FromCollection, WithStyles
{
    protected $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * 表头
     * @return string[]
     */
    public function headings(): array
    {
        $hospitalName = Hospital::query()->where('id',$this->data['hospital_id'])->value('name');
        return [
            ["{$hospitalName}0-36月儿童中医药健康管理服务台账"],
            ['序号', '姓名', '月龄', '电话', '指导日期'],
        ];
    }

    /**
     * 导出
     * @return \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection
     */
    public function collection()
    {
        $data = $this->data;
        $i=1;
        $hospital_id = $data['hospital_id'];
        $sql = "SET sql_mode = (SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));";
        DB::statement($sql);
        $query = FamilyEdu::query()->with(['user','family','familyType'])->where('hospital_id',$hospital_id);
        if (isset($data['start'])) {
            $query->where('created_at', '>=', $data['start']);
        }
        if (isset($data['end'])) {
            $query->where('created_at', '<=', $data['end']);
        }
        $models = $query->groupBy('family_id','family_type_id')->get();


        $list = [];
        foreach($models as $model) {
            //dd($sign);
            $family = $model->family;
            $user = $model->user;
            $familyType = $model->familyType;
            $row = [];
            $row[] = $i++;
            $row[] = isset($family)?$family->name:"";
            $row[] = isset($familyType)?$familyType->name:"";
            $row[] = isset($user)?$user->phone:"";
            $date = Carbon::parse($model->created_at);
            $formattedDate = $date->format('Y-m-d'); // 输出: 2023-10-05
            $row[] = $formattedDate;
            $list[] = $row;
        }
        $this->column = $i;
        return collect($list);
    }

    public function styles(Worksheet $sheet)
    {
        $sheet->mergeCells('A1:E1');



        $sheet->getStyle('A1:E1')->getAlignment()->setVertical('center')->setHorizontal('center');//垂直居中
        $sheet->getStyle('A1:E' . ($this->column + 2))->getAlignment()->setVertical('center')->setHorizontal('center');//垂直居中
        $sheet->getRowDimension('1')->setRowHeight(25);//设置指定行高
        $sheet->getDefaultRowDimension()->setRowHeight(22);//设置默认行高


        $sheet->getDefaultColumnDimension()->setWidth(10);
        $sheet->getColumnDimension('D')->setWidth(20);
        $sheet->getColumnDimension('E')->setWidth(20);
        $styleArray = [
            'borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN //细边框
                ]
            ]
        ];
        $sheet->getStyle('A1:E' . ($this->column + 1))->applyFromArray($styleArray);
    }
}
